package v2;

import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import Kernel.*;

public class Chargement {

	public static void Charger(Serveur serveur) throws IOException {
		BufferedReader bufferTrajet = new BufferedReader(new FileReader(
				"fichierTrajet"));
		BufferedReader bufferVille = new BufferedReader(new FileReader(
				"fichierVille"));
		BufferedReader bufferVehicule = new BufferedReader(new FileReader(
				"fichierVehicule"));
		BufferedReader bufferLog = new BufferedReader(new FileReader(
				"fichierLog"));
		String accu = new String();
		String tab2[];

		/* Boucle Restauration Vehicule */

		while (bufferVehicule.ready()) {
			String accu2 = bufferVehicule.readLine();
			tab2 = accu2.split(";");
			serveur.ajouterVehicule(Chargement.hashtoVehicule(tab2,0));
		}

		/* Boucle Restauration Ville */
		accu = new String();
		while (bufferVille.ready()) {
			String accu2 = bufferVille.readLine();
			tab2 = accu2.split(";");
			serveur.ajouterVille(v2.Chargement.hashtoVille(tab2, 0));
		}

		/* Boucle Restauration Log */

		while (bufferLog.ready())
			serveur.ajouterLog(bufferLog.readLine());

		/* Boucle Restauration Trajet */

		accu = new String();
		while (bufferTrajet.ready()) {
			accu = bufferTrajet.readLine();
			tab2 = accu.split(";");
			/*
			 * Trajet t = new Trajet(tab2[0],Chargement.hashtoVehicule(tab2,1),
			 * Chargement.hashtoVille(tab2, 5), new Horaire(new Horaires(
			 * Integer.parseInt(tab2[14]), Integer.parseInt(tab2[15]),
			 * Integer.parseInt(tab2[16])), new Date(
			 * Integer.parseInt(tab2[11]), Integer.parseInt(tab2[12]),
			 * Integer.parseInt(tab2[13]))), Chargement.hashtoVille(tab2, 17),
			 * new Horaire(new Horaires( Integer.parseInt(tab2[26]),
			 * Integer.parseInt(tab2[27]), Integer.parseInt(tab2[28])), new
			 * Date( Integer.parseInt(tab2[23]), Integer.parseInt(tab2[24]),
			 * Integer.parseInt(tab2[25]))));
			 */
			Trajet t = new Trajet(tab2[0], Chargement.hashtoVehicule(tab2, 1),
					Chargement.hashtoVille(tab2, 5), new GregorianCalendar(
							Integer.parseInt(tab2[13]), Integer
									.parseInt(tab2[12]), Integer
									.parseInt(tab2[11]), Integer
									.parseInt(tab2[14]), Integer
									.parseInt(tab2[15])), Chargement
							.hashtoVille(tab2, 16), new GregorianCalendar(
							Integer.parseInt(tab2[24]), Integer
									.parseInt(tab2[23]), Integer
									.parseInt(tab2[22]), Integer
									.parseInt(tab2[25]), Integer
									.parseInt(tab2[26])), Integer
							.parseInt(tab2[27]), tab2[28]);
			serveur.ajouterTrajet(t);
		}
		bufferTrajet.close();
		bufferVehicule.close();
		bufferVille.close();
		bufferLog.close();
	}

	private static Vehicule hashtoVehicule(String[] tab2, int indiceStart) {
		Vehicule vehicule = null;
		if (tab2[indiceStart].compareTo("bus") == 0) {
			vehicule = new Bus(tab2[indiceStart+1],Integer.parseInt(tab2[indiceStart+2]),
					Integer.parseInt(tab2[indiceStart+3]));
		} else if (tab2[indiceStart].compareTo("avion") == 0) {
			vehicule = new Avion(tab2[indiceStart+1],Integer.parseInt(tab2[indiceStart+2]),
					Integer.parseInt(tab2[indiceStart+3]));
		} else if (tab2[indiceStart].compareTo("train") == 0) {
			vehicule = new Train(tab2[indiceStart+1],Integer.parseInt(tab2[indiceStart+2]),
					Integer.parseInt(tab2[indiceStart+3]));
		} else if (tab2[indiceStart].compareTo("bateau") == 0) {
			vehicule = new Bateau(tab2[indiceStart+1],Integer.parseInt(tab2[indiceStart+2]),
					Integer.parseInt(tab2[indiceStart+3]));
		} else if (tab2[indiceStart].compareTo("generique") == 0) {
			vehicule = new VehiculeGenerique(tab2[indiceStart+1],Integer.parseInt(tab2[indiceStart+2]),
					Integer.parseInt(tab2[indiceStart+3]));
		}
		return vehicule;
	}

	private static Ville hashtoVille(String[] tab2, int indiceStart) {
		return new Ville(tab2[indiceStart],
				Long.parseLong(tab2[indiceStart + 1]),
				Boolean.parseBoolean(tab2[indiceStart + 2]),
				Boolean.parseBoolean(tab2[indiceStart + 3]),
				Boolean.parseBoolean(tab2[indiceStart + 4]),
				Boolean.parseBoolean(tab2[indiceStart + 5]));
	}

	public static ArrayList<String> exportSimple(Serveur serveur)
			throws IOException {

		ArrayList<String> accu = new ArrayList<String>();
		String str = "";
		String str2 = "";
		String str3 = "";
		String str4 = "";

		BufferedReader bufferTrajet = new BufferedReader(new FileReader(
				"fichierTrajet"));
		BufferedReader bufferVille = new BufferedReader(new FileReader(
				"fichierVille"));
		BufferedReader bufferVehicule = new BufferedReader(new FileReader(
				"fichierVehicule"));
		BufferedReader bufferLog = new BufferedReader(new FileReader(
				"fichierLog"));

		while (bufferTrajet.ready()) {
			str += bufferTrajet.readLine() + "\n";
		}
		while (bufferVille.ready()) {
			str2 += bufferVille.readLine() + "\n";
		}
		while (bufferVehicule.ready()) {
			str3 += bufferVehicule.readLine() + "\n";
		}
		while (bufferLog.ready()) {
			str4 += bufferLog.readLine() + "\n";
		}

		accu.add(str);
		accu.add(str2);
		accu.add(str3);
		accu.add(str4);

		return accu;

	}
}
